﻿using System;
using System.Collections.Generic;
using Lua;
using MoonSharp.Interpreter;
using UnityEngine;

// Token: 0x020001FB RID: 507
public class LuaBehavior : IBehavior
{
	// Token: 0x06000ACE RID: 2766 RVA: 0x0005BB34 File Offset: 0x00059D34
	public LuaBehavior(Table t, Script s)
	{
		this.script = s;
		this.table = t;
		this.flags = new List<string>();
		this.settings = new List<LuaBehavior.BehaviorSetting>();
		this.agentDef.exclude = new List<TargetType>();
		this.agentDef.include = new List<TargetType>();
		this.targetDef.exclude = new List<TargetType>();
		this.targetDef.include = new List<TargetType>();
		foreach (TablePair tablePair in this.table.Pairs)
		{
			if (tablePair.Key.Type == DataType.String)
			{
				string @string = tablePair.Key.String;
				if (@string == "name")
				{
					this.internalName = tablePair.Value.String;
				}
				else if (@string == "data")
				{
					this.ParseData(tablePair.Value.Table);
				}
				else if (@string == "agentType")
				{
					this.agentDef.include.Add((TargetType)Enum.Parse(typeof(TargetType), tablePair.Value.String, true));
				}
				else if (@string == "targetType")
				{
					this.targetDef.include.Add((TargetType)Enum.Parse(typeof(TargetType), tablePair.Value.String, true));
				}
				else if (@string == "scores")
				{
					this.AddScores(tablePair.Value);
				}
				else if (@string == "react")
				{
					this.react = tablePair.Value.Boolean;
				}
				else if (@string == "hidden")
				{
					this.hidden = tablePair.Value.Boolean;
				}
			}
		}
		IOManager.Instance.LoadSettings(this);
		if (this.targetDef.include.Count == 0)
		{
			this.targetDef.include.Add(TargetType.None);
		}
		this.functionCreateInstance = this.script.Globals.Get("CreateInstance");
		if (this.ai)
		{
			this.canUseAi = true;
		}
	}

	// Token: 0x06000ACF RID: 2767 RVA: 0x0005BDE8 File Offset: 0x00059FE8
	private DynValue GetMethod(Table t, string methodName)
	{
		DynValue dynValue = t.Get(methodName);
		if (dynValue.IsNil())
		{
			dynValue = t.MetaTable.Get(methodName);
			if (dynValue.IsNil())
			{
				dynValue = t.MetaTable.Get("__index").Table.MetaTable.Get(methodName);
			}
		}
		return dynValue;
	}

	// Token: 0x06000AD0 RID: 2768 RVA: 0x0005BE44 File Offset: 0x0005A044
	private void RegisterSetting(Table s)
	{
		if (s.Get(1).String.Length == 0 || s.Get(1).String.Contains(" ") || ((s.Get(1).String.ToLower()[0] < 'a' || s.Get(1).String.ToLower()[0] > 'z') && s.Get(1).String.ToLower()[0] != '_'))
		{
			Debug.LogError(string.Concat(new string[]
			{
				"RegisterSetting() - Failed to register setting \"",
				s.Get(1).String,
				"\" for behavior \"",
				this.text,
				"\" (Invalid variable name)"
			}));
			return;
		}
		if (s.Get(2).String.Length == 0 || s.Get(2).String.Replace(" ", string.Empty).Length == 0)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"RegisterSetting() - Failed to register setting \"",
				s.Get(2).String,
				"\" for behavior \"",
				this.text,
				"\" (Invalid UI text)"
			}));
			return;
		}
		LuaBehavior.BehaviorSetting item = default(LuaBehavior.BehaviorSetting);
		try
		{
			item.varName = s.Get(1).String;
			item.uiText = s.Get(2).String;
			item.type = (LuaBehavior.BehaviorSettingType)Enum.Parse(typeof(LuaBehavior.BehaviorSettingType), s.Get(3).String, true);
			switch (item.type)
			{
			case LuaBehavior.BehaviorSettingType.Bool:
				item.value = DynValue.NewBoolean(s.Get(4).Boolean);
				break;
			case LuaBehavior.BehaviorSettingType.String:
				item.value = DynValue.NewString(s.Get(4).String);
				break;
			case LuaBehavior.BehaviorSettingType.Float:
				item.value = DynValue.NewNumber(s.Get(4).Number);
				break;
			case LuaBehavior.BehaviorSettingType.Array:
				item.value = DynValue.NewNumber(s.Get(4).Number);
				item.array = DynValue.NewTable(s.Get(5).Table);
				break;
			case LuaBehavior.BehaviorSettingType.Keybind:
				try
				{
					UnityEngine.Input.GetKey(s.Get(4).String[0].ToString());
					item.value = DynValue.NewString(s.Get(4).String[0].ToString());
				}
				catch
				{
					Debug.LogError(string.Concat(new string[]
					{
						"RegisterSetting() - Failed to register setting \"",
						s.Get(1).String,
						"\" for behavior \"",
						this.text,
						"\" (Invalid default keybind)"
					}));
					return;
				}
				break;
			}
			this.settings.Add(item);
		}
		catch
		{
			Debug.LogError(string.Concat(new string[]
			{
				"RegisterSetting() - Failed to register setting \"",
				s.Get(1).String,
				"\" for behavior \"",
				this.text,
				"\" (Bad settingType \"",
				s.Get(3).String,
				"\")"
			}));
		}
	}

	// Token: 0x06000AD1 RID: 2769 RVA: 0x0005C1E8 File Offset: 0x0005A3E8
	private void ParseData(Table data)
	{
		foreach (TablePair tablePair in data.Pairs)
		{
			string @string = tablePair.Key.String;
			if (@string == "secondary")
			{
				this.secondary = tablePair.Value.Boolean;
			}
			else if (@string == "flags")
			{
				foreach (TablePair tablePair2 in tablePair.Value.Table.Pairs)
				{
					this.flags.Add(tablePair2.Value.String);
				}
				this.secondary = true;
			}
			else if (@string == "canUseAI" || @string == "ai")
			{
				this.canUseAi = (@string == "ai" || tablePair.Value.Boolean);
			}
			else if (@string == "menuEntry")
			{
				this.text = tablePair.Value.String;
			}
			else if (@string == "agent")
			{
				this.agentDef = this.ParseEntityDef(tablePair.Value.Table, this.agentDef);
			}
			else if (@string == "target")
			{
				this.targetDef = this.ParseEntityDef(tablePair.Value.Table, this.targetDef);
			}
			else if (@string == "hideMenu")
			{
				this.hidden = tablePair.Value.Boolean;
			}
			else if (@string == "settings")
			{
				foreach (TablePair tablePair3 in tablePair.Value.Table.Pairs)
				{
					Table s = tablePair3.Value.Table;
					this.RegisterSetting(s);
				}
			}
			else if (@string == "forceAppearInManager")
			{
				this.forceAppearInManager = tablePair.Value.Boolean;
			}
		}
	}

	// Token: 0x06000AD2 RID: 2770 RVA: 0x0005C4B8 File Offset: 0x0005A6B8
	private EntityDef ParseEntityDef(Table data, EntityDef def)
	{
		foreach (TablePair tablePair in data.Pairs)
		{
			string @string = tablePair.Key.String;
			if (@string == "type")
			{
				foreach (TablePair tablePair2 in tablePair.Value.Table.Pairs)
				{
					def.include.Add((TargetType)Enum.Parse(typeof(TargetType), tablePair2.Value.String, true));
				}
			}
			else if (@string == "exclude")
			{
				foreach (TablePair tablePair3 in tablePair.Value.Table.Pairs)
				{
					def.exclude.Add((TargetType)Enum.Parse(typeof(TargetType), tablePair3.Value.String, true));
				}
			}
		}
		return def;
	}

	// Token: 0x06000AD3 RID: 2771 RVA: 0x0005C660 File Offset: 0x0005A860
	public IBehaviorInstance CreateInstance(EntityBase agent, EntityBase target, UnityEngine.Vector3 cursorPoint)
	{
		LuaBehaviorInstance luaBehaviorInstance = null;
		Entity luaEntity = agent.GetLuaEntity();
		Entity entity = null;
		if (target != null)
		{
			entity = target.GetLuaEntity();
		}
		try
		{
			DynValue dynValue = this.script.Call(this.functionCreateInstance, new object[]
			{
				this.internalName,
				luaEntity,
				entity,
				new Lua.Vector3(cursorPoint)
			});
			luaBehaviorInstance = new LuaBehaviorInstance(dynValue.Table, this.script);
			luaBehaviorInstance.behaviorFilename = this.filename;
			this.UpdateInstanceScriptSettings(luaBehaviorInstance);
		}
		catch (ScriptRuntimeException ex)
		{
			Debug.LogError(ex.DecoratedMessage);
		}
		return luaBehaviorInstance;
	}

	// Token: 0x06000AD4 RID: 2772 RVA: 0x0005C70C File Offset: 0x0005A90C
	public void UpdateInstanceScriptSettings(LuaBehaviorInstance inst)
	{
		this.settings.ForEach(delegate(LuaBehavior.BehaviorSetting setting)
		{
			inst.instance.Set(setting.varName, setting.value);
		});
	}

	// Token: 0x06000AD5 RID: 2773 RVA: 0x0005C73D File Offset: 0x0005A93D
	public bool CanAppearInBehaviorManager()
	{
		return !this.hidden || this.forceAppearInManager;
	}

	// Token: 0x06000AD6 RID: 2774 RVA: 0x0005C753 File Offset: 0x0005A953
	public bool IsReactive()
	{
		return this.react;
	}

	// Token: 0x06000AD7 RID: 2775 RVA: 0x0005C75B File Offset: 0x0005A95B
	public bool IsHidden()
	{
		return this.hidden || !this.enabled;
	}

	// Token: 0x06000AD8 RID: 2776 RVA: 0x0005C774 File Offset: 0x0005A974
	public string GetName()
	{
		return this.internalName;
	}

	// Token: 0x06000AD9 RID: 2777 RVA: 0x0005C77C File Offset: 0x0005A97C
	public EntityDef GetAgentDef()
	{
		return this.agentDef;
	}

	// Token: 0x06000ADA RID: 2778 RVA: 0x0005C784 File Offset: 0x0005A984
	public EntityDef GetTargetDef()
	{
		return this.targetDef;
	}

	// Token: 0x06000ADB RID: 2779 RVA: 0x0005C78C File Offset: 0x0005A98C
	public string GetText()
	{
		if (this.text.Length == 0)
		{
			return this.GetName();
		}
		return this.text;
	}

	// Token: 0x06000ADC RID: 2780 RVA: 0x0005C7AB File Offset: 0x0005A9AB
	public bool IsSecondary()
	{
		return this.secondary;
	}

	// Token: 0x06000ADD RID: 2781 RVA: 0x0005C7B3 File Offset: 0x0005A9B3
	public bool IsEnabled()
	{
		return this.enabled;
	}

	// Token: 0x06000ADE RID: 2782 RVA: 0x0005C7BB File Offset: 0x0005A9BB
	public bool IsAI()
	{
		return this.ai;
	}

	// Token: 0x06000ADF RID: 2783 RVA: 0x0005C7C3 File Offset: 0x0005A9C3
	public bool CanUseAI()
	{
		return this.canUseAi;
	}

	// Token: 0x06000AE0 RID: 2784 RVA: 0x0005C7CB File Offset: 0x0005A9CB
	public List<string> GetFlags()
	{
		return this.flags;
	}

	// Token: 0x06000AE1 RID: 2785 RVA: 0x0005C7D4 File Offset: 0x0005A9D4
	private void AddScores(DynValue value)
	{
		if (value.Type != DataType.Table)
		{
			Debug.LogError("behavior.scores must be in a table");
			return;
		}
		this.scores = new List<BehaviorScore>();
		foreach (TablePair tablePair in value.Table.Pairs)
		{
			string @string = tablePair.Key.String;
			float val = (float)tablePair.Value.Number;
			try
			{
				BehaviorScore item = new BehaviorScore(@string, val);
				this.scores.Add(item);
			}
			catch
			{
				Debug.LogWarning(this.filename + " " + @string + " is not an valid score");
			}
		}
		this.ai = true;
	}

	// Token: 0x04000FD4 RID: 4052
	public bool enabled = true;

	// Token: 0x04000FD5 RID: 4053
	public bool forceAppearInManager;

	// Token: 0x04000FD6 RID: 4054
	public string filename = string.Empty;

	// Token: 0x04000FD7 RID: 4055
	private string internalName;

	// Token: 0x04000FD8 RID: 4056
	private string text = string.Empty;

	// Token: 0x04000FD9 RID: 4057
	private EntityDef agentDef;

	// Token: 0x04000FDA RID: 4058
	private EntityDef targetDef;

	// Token: 0x04000FDB RID: 4059
	public bool ai;

	// Token: 0x04000FDC RID: 4060
	public bool canUseAi;

	// Token: 0x04000FDD RID: 4061
	private bool secondary;

	// Token: 0x04000FDE RID: 4062
	private List<string> flags;

	// Token: 0x04000FDF RID: 4063
	public Script script;

	// Token: 0x04000FE0 RID: 4064
	public List<BehaviorScore> scores;

	// Token: 0x04000FE1 RID: 4065
	public List<LuaBehavior.BehaviorSetting> settings;

	// Token: 0x04000FE2 RID: 4066
	private bool react;

	// Token: 0x04000FE3 RID: 4067
	public bool hidden;

	// Token: 0x04000FE4 RID: 4068
	private Table table;

	// Token: 0x04000FE5 RID: 4069
	private DynValue functionCreateInstance;

	// Token: 0x020001FC RID: 508
	public enum BehaviorSettingType
	{
		// Token: 0x04000FE7 RID: 4071
		Bool = 1,
		// Token: 0x04000FE8 RID: 4072
		String,
		// Token: 0x04000FE9 RID: 4073
		Float,
		// Token: 0x04000FEA RID: 4074
		Array,
		// Token: 0x04000FEB RID: 4075
		Keybind
	}

	// Token: 0x020001FD RID: 509
	public struct BehaviorSetting
	{
		// Token: 0x04000FEC RID: 4076
		public string varName;

		// Token: 0x04000FED RID: 4077
		public string uiText;

		// Token: 0x04000FEE RID: 4078
		public LuaBehavior.BehaviorSettingType type;

		// Token: 0x04000FEF RID: 4079
		public DynValue value;

		// Token: 0x04000FF0 RID: 4080
		public DynValue array;
	}
}
